package com.itheima.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.statusParamDto;
import com.itheima.mapper.OmsOrderReturnApplyMapper;
import com.itheima.pojo.OmsOrderReturnApply;
import com.itheima.vo.MinioVo;
import com.itheima.vo.OmsVo.ReturnApplyListVo;
import com.itheima.vo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.bouncycastle.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 芝士雪豹
 * @create 2022/10/15/0:39
 */
@DubboService
public class OmsOrderReturnApplyAPiImpl implements OmsOrderReturnApplyApi{
    @Autowired
    private OmsOrderReturnApplyMapper mapper;

    @Override
    public MinioVo delete(Integer[] ids) {
        MinioVo vo = new MinioVo();
        for (Integer id : ids) {
            int deleteById = mapper.deleteById(id);
            if (deleteById==0){
                vo.setCode(404);
                vo.setDate(null);
                vo.setMessage("暂无数据删除");
                return vo;
            }
        }
        vo.setCode(200);
        vo.setDate(null);
        vo.setMessage("删除申请成功");
        return vo;

    }

    @Override
    public PageResult list(ReturnApplyListVo vo) {
        LambdaQueryWrapper<OmsOrderReturnApply> wrapper = new LambdaQueryWrapper<>();
        boolean matches = Pattern.compile("18[0-9]{9}").matcher(vo.getReceiverKeyword()).matches();
        wrapper.eq(vo.getCreateTime()!=null,OmsOrderReturnApply::getCreateTime,vo.getCreateTime())
                .eq(vo.getId()!=null,OmsOrderReturnApply::getId,vo.getId())
                .eq(vo.getHandleTime()!=null,OmsOrderReturnApply::getHandleTime,vo.getHandleTime())
                .eq(vo.getStatus()!=null,OmsOrderReturnApply::getStatus,vo.getStatus())
                .eq(vo.getHandleMan()!=null,OmsOrderReturnApply::getHandleMan,vo.getHandleMan())
                .eq(StrUtil.isNotBlank(vo.getReceiverKeyword()),matches ? OmsOrderReturnApply::getReturnPhone : OmsOrderReturnApply::getReturnName,vo.getId());
        Page pageInfo =new Page(vo.getPageNum(),vo.getPageSize());
        mapper.selectPage(pageInfo,wrapper);
        return new PageResult<>(Convert.toInt(pageInfo.getCurrent()), Convert.toInt(pageInfo.getSize()), Convert.toInt(pageInfo.getTotal()), Convert.toInt(pageInfo.getCurrent()), pageInfo.getRecords());
    }

    @Override
    public MinioVo updateStatus(Integer id, statusParamDto dto) {
        LambdaUpdateWrapper<OmsOrderReturnApply> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(id!=null,OmsOrderReturnApply::getId,id)
                .set(dto.getId()!=null,OmsOrderReturnApply::getId,dto.getId())
                .set(dto.getStatus()!=null,OmsOrderReturnApply::getStatus,dto.getStatus())
                .set(dto.getHandleNote()!=null,OmsOrderReturnApply::getHandleNote,dto.getHandleNote())
                .set(dto.getCompanyAddressId()!=null,OmsOrderReturnApply::getCompanyAddressId,dto.getCompanyAddressId())
                .set(dto.getHandleMan()!=null,OmsOrderReturnApply::getHandleMan,dto.getHandleMan())
                .set(dto.getReceiveMan()!=null,OmsOrderReturnApply::getReceiveMan,dto.getReceiveMan())
                .set(dto.getReturnAmount()!=null,OmsOrderReturnApply::getReturnAmount,dto.getReturnAmount())
                .set(dto.getReceiveNote()!=null,OmsOrderReturnApply::getReceiveNote,dto.getReceiveNote());
        int update = mapper.update(null, wrapper);
        MinioVo vo = new MinioVo();
        if (update==0){
            vo.setCode(500);
            vo.setDate(null);
            vo.setMessage("暂无数据修改");
            return vo;
        }
        vo.setCode(200);
        vo.setDate(null);
        vo.setMessage("数据修改成功!");
        return vo;
    }

    @Override
    public MinioVo findById(Integer id) {
        OmsOrderReturnApply omsOrderReturnApply = mapper.selectById(id);
        MinioVo vo = new MinioVo();
        if (omsOrderReturnApply!=null){
            vo.setCode(200);
            vo.setDate(omsOrderReturnApply);
            vo.setMessage("查询成功!");
            return vo;
        }
        vo.setCode(500);
        vo.setDate(null);
        vo.setMessage("暂无数据");
        return vo;
    }
}
